package com.gyy.androidlibraryapp.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyy.androidlibraryapp.R;
import com.gyy.androidlibraryapp.beans.CompanyInfoBean;
import com.gyy.androidlibraryapp.utils.DataHelper;
import com.gyy.androidlibraryapp.utils.DateHelper;
import com.ymdd.library.pickerview.OptionsPickerView;
import com.ymdd.library.pickerview.TimePickerView;
import com.ymdd.library.pickerview.WeekTimePickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 时间日期选择, 自定义选项卡
 */
public class PickerViewFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.time)
    TextView tv_time;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_picker, null);

        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.bt_1, R.id.bt_2, R.id.bt_3, R.id.bt_4, R.id.bt_5})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_1:
                showTime(DateHelper.ALL, TimePickerView.Type.ALL);
                break;
            case R.id.bt_2:
                showTime(DateHelper.YEAR_MONTH_DAY, TimePickerView.Type.YEAR_MONTH_DAY);
                break;
            case R.id.bt_3:
                showWeekTime();
                break;
            case R.id.bt_4:
                showOptionsPicke();
                break;
            case R.id.bt_5:
                showOptionsPicke3();
                break;
        }
    }

    /**
     * WheelView.dividerColor 可修改分割线颜色
     *
     * 显示时间，标签显示（年，月，日，时，分，秒）
     * @param format 时间格式
     * @param type 显示类型
     */
    private void showTime(final String format, final TimePickerView.Type type) {
        //当前时间 (也可以设置任意时间)
        long selectTextTime = new Date().getTime();
        Calendar calendarCurrent = Calendar.getInstance();
        calendarCurrent.setTimeInMillis(selectTextTime);
        //时间控件
        TimePickerView pickerView = new TimePickerView.Builder(this.getContext(), new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                //选择结果
                String time = DateHelper.getDateToString(date.getTime(), format);
                tv_time.setText(time);
            }
        })
                .setType(type)
                .setDate(calendarCurrent)
                .isCyclic(true)
                .setContentSize(20)
                .setLabel("年", "月", "日", "时", "分", "秒")
                .build();
        pickerView.show();
    }

    /**
     * PickerConfig.addDay = 10 (当前时间，向后推10天)
     * 修改改值，可调整显示的天数
     */
    private void showWeekTime() {
        WeekTimePickerView timePickerView = new WeekTimePickerView.Builder(this.getContext(), new WeekTimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(String time, View v) {
                //time的格式是 yyyy-MM-dd HH:mm
                Date date = DateHelper.getDate(time, DateHelper.MINUTE_FORMAT);
                String stringDate = DateHelper.getStringDate(date, DateHelper.MINUTE_FORMAT);
                tv_time.setText(stringDate);
            }
        })
                .setMinMillseconds(System.currentTimeMillis())
                .setTitleText("请选择揽货时间")
                .build();
        timePickerView.show();
    }

    /**
     * 一级 条件选择器
     */
    public void showOptionsPicke() {
        //数据，CompanyInfoBean 需实现 IPickerViewData
       final List<CompanyInfoBean> companyInfoBeans = DataHelper.buildData();

        OptionsPickerView lineoptions = new OptionsPickerView.Builder(getContext(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                // 返回的分别是三个级别的选中位置
                String compName = companyInfoBeans.get(options1).getCompName();
                tv_time.setText(compName);

            }
        }).setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setContentTextSize(26)//滚轮文字大小
                .setCyclic(false, false, false)//循环与否
//                .setSelectOptions(options1)  //设置默认选中项
                .setTitleText("选择公司")
                .isDialog(false)//是否显示为对话框样式
                .setLineSpacingMultiplier(1.7f)//设置间距倍数
                .isDividerGone(true)//不显示分割线
                .build();
        lineoptions.setPicker(companyInfoBeans);//添加数据源
        lineoptions.show();


    }


    /**
     * 3级 条件选择器
     */
    public void showOptionsPicke3() {
        //三级选择器的 数据，和每一级的item条数
        int optionSize1 = 3;
        int optionSize2 = 4;
        int optionSize3 = 5;
        List<CompanyInfoBean> optionList1 = new ArrayList<>();
        List<List<CompanyInfoBean>> optionList2 = new ArrayList<>();
        List<List<List<CompanyInfoBean>>> optionList3 = new ArrayList<>();

        //构建optionList1，一级数据
        for (int i = 0; i < optionSize1; i++) {
            optionList1.add(new CompanyInfoBean(i+ "一级 "));
        }
        //构建optionList2，二级数据
        for (int i = 0; i < optionSize1; i++) {
            List<CompanyInfoBean> temp = new ArrayList<>();
            for (int j = 0; j < optionSize2; j++) {
                temp.add(new CompanyInfoBean(i + "二级  " + j));
            }
            optionList2.add(temp);
        }
        //构建optionList3，三级数据
        for (int i = 0; i < optionSize1; i++) {
            List<List<CompanyInfoBean>> temp = new ArrayList<>();
            for (int j = 0; j < optionSize2; j++) {
                List<CompanyInfoBean> list = new ArrayList<>();
                for (int k = 0; k < optionSize3; k++) {
                    list.add(new CompanyInfoBean(i + "三级  " + k));
                }
                temp.add(list);
            }
            optionList3.add(temp);
        }


        OptionsPickerView lineoptions = new OptionsPickerView.Builder(getContext(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                // 返回的分别是三个级别的选中位置
//                String compName = optionList1.get(options1).getCompName();
                tv_time.setText(options1 + " " + options2 + " " + options3);

            }
        }).setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setContentTextSize(26)//滚轮文字大小
                .setCyclic(false, false, false)//循环与否
//                .setSelectOptions(options1, options2, options3)  //设置默认选中项
                .setTitleText("选择公司")
                .isDialog(false)//是否显示为对话框样式
                .setLineSpacingMultiplier(1.7f)//设置间距倍数
                .build();
        lineoptions.setPicker(optionList1, optionList2, optionList3);//添加数据源
        lineoptions.show();


    }

}